﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogicServer.Users;
using Model;
using CommonCs;

namespace BugSysSolution.RequestAjaxPage
{
    public partial class UserAjaxPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String tag = Request["tag"] ?? String.Empty;
            if (tag == "checkuid")
            {
                CheckExist();
            }
            if (tag == "adduser")
            {
                AddUser();
            }
            if (tag=="del")
            {
                DeleteUser();
            }
        }

        public void CheckExist()
        {
            String uid = Request.Form["uid"].ToLower();
            UserLogic ul = new UserLogic();
            Users user = ul.GetUserByUid(uid);
            if (user == null)
            {
                Response.Write("Success");
            }
            else
            {
                Response.Write("Error");
            }
        }

        public void AddUser() 
        {
            Users _user = Session["user"] as Users;
            UserLogic ul = new UserLogic();
            Users user = new Users();
            user.Uid = Request.Form["uid"].ToLower();
            user.PassWord = Request.Form["PassWord"].ToString();
            user.Email = Request.Form["Email"].ToString();
            user.QQ = Request.Form["QQ"].ToString();
            user.RealName = Request.Form["RealName"].ToString();
            user.Hobby = Request.Form["Hobby"].ToString();
            user.Sinaweibo = Request.Form["Sinaweibo"].ToString();

            int result = ul.AddUser(user);
            if (result != 1)
            {
                Common.WriteFile(CommonCs.Exceptions.Operational, _user.Uid, "添加用户[" + user.Uid + "]失败!");
                Response.Write("1|添加失败");
            }
            else
            {
                Common.WriteFile(CommonCs.Exceptions.Operational, _user.Uid, "添加用户[" + user.Uid + "]成功!");
                Response.Write("0|添加成功");
            }
        }

        public void DeleteUser()
        {
            Users _user = Session["user"] as Users;
            String id = Request.Form["id"].ToString() ?? String.Empty;
            UserLogic ul = new UserLogic();
            int result = ul.DeleteUser(id);
            if (result != 1)
            {
                Common.WriteFile(CommonCs.Exceptions.Operational, _user.Uid, "删除用户,ID为[" + id + "]失败!");
                Response.Write("Error");
            }
            else
            {
                Common.WriteFile(CommonCs.Exceptions.Operational, _user.Uid, "删除用户,ID为[" + id + "]成功!");
                Response.Write("Success");
            }
        }
    }
}